package git.bds.nyc.admin.service;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import git.bds.nyc.communal.mapper.mp.ContractOrderMapper;
import git.bds.nyc.communal.model.domain.Address;
import git.bds.nyc.communal.model.domain.ContractOrder;
import git.bds.nyc.admin.model.dto.OrderResultDTO;
import git.bds.nyc.product.model.domain.ProductCollection;
import git.bds.nyc.result.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 成大事
 * @since 2023/5/14 15:07
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class OrderManageServiceImpl implements OrderManageService{


    private final ContractOrderMapper contractOrderMapper;


    /**
     * 按日期导出订单列表
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param type      类型
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public List<OrderResultDTO> exportOrderListByDate(int type,LocalDateTime startTime, LocalDateTime endTime) {
       return contractOrderMapper.selectJoinList(OrderResultDTO.class,new MPJLambdaWrapper<ContractOrder>()
                .select(ContractOrder::getProductSpecies,ContractOrder::getProductVariety,ContractOrder::getType,
                        ContractOrder::getUnitPrice,ContractOrder::getOrderWeight,ContractOrder::getOrderStatus,
                        ContractOrder::getCreateTime,ContractOrder::getCompleteTime)

                .selectAs("seller_info", Address::getConsignee, OrderResultDTO::getSellerName)
                .selectAs("seller_info", Address::getPhone, OrderResultDTO::getSellerPhone)
                .selectAs("seller_info", Address::getLocation, OrderResultDTO::getSellerLocation)
                .selectAs("seller_info", Address::getDetailedAddress, OrderResultDTO::getSellerDetailedAddress)

                .selectAs("buyer_info", Address::getConsignee, OrderResultDTO::getBuyerName)
                .selectAs("buyer_info", Address::getPhone, OrderResultDTO::getBuyerPhone)
                .selectAs("buyer_info", Address::getLocation, OrderResultDTO::getBuyerLocation)
                .selectAs("buyer_info", Address::getDetailedAddress, OrderResultDTO::getBuyerDetailedAddress)

                .innerJoin(Address.class,"seller_info",Address::getId,ContractOrder::getSellerContactInfoId)
                .innerJoin(Address.class,"buyer_info",Address::getId,ContractOrder::getBuyerContactInfoId)
                .eq(ContractOrder::getOrderStatus,type)
                .between(ProductCollection::getCreateTime,startTime.withHour(0).withMinute(0).withSecond(0),endTime.withHour(23).withMinute(59).withSecond(59))
                .orderByDesc(ContractOrder::getCompleteTime));
    }
}
